<?php
require '../../framework/bootstrap.inc.php';
require IA_ROOT.'/addons/goouc_class/inc/common/WxPayJsApiPay.php';
global $_W;

$xml = file_get_contents("php://input");
libxml_disable_entity_loader(true);
$xmlstr = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
$data = json_decode(json_encode($xmlstr),true);
if($data['return_code'] == "SUCCESS"){
	$data_old_sign = $data['sign'];
	unset($data['sign']);
	$uniacid = pdo_get('goouc_class_order',array('out_trade_no'=>$data['out_trade_no']));
	$pay_set = pdo_get('uni_account_modules',array('uniacid'=>$uniacid['weid'],'module'=>'goouc_class'));
	$payset = unserialize($pay_set['settings']);
	$new_sign = getSign($data,$payset['pay_secret']);

	if($new_sign == $data_old_sign && $data['return_code'] == "SUCCESS"){
		$a = array('order_status'=>1,'paytime'=>time(),'transaction_sn'=>$data['transaction_id']);
		$res = pdo_update('goouc_class_order',$a,array('weid'=>$uniacid['weid'],'out_trade_no'=>$data['out_trade_no']));
		if($res){
		   /* if($uniacid['type'] == 1){
                pdo_query("update " . tablename('goouc_class_lesson') . " set pay_num = pay_num + 1 where id=:id", array(":id" => $uniacid['dataid']));
            }elseif($uniacid['type'] == 2){
                pdo_query("update " . tablename('goouc_class_son') . " set pay_num = pay_num + 1 where id=:id", array(":id" => $uniacid['dataid']));
            }elseif($uniacid['type'] == 3){
                pdo_query("update " . tablename('goouc_class_user') . " set card = card + ".$uniacid['dataid']." where id=:id", array(":id" => $uniacid['userid']));
            }elseif($uniacid['type'] == 4){
                pdo_query("update " . tablename('goouc_class_user') . " set card = card + ".$uniacid['dataid']." where id=:id", array(":id" => $uniacid['userid']));
            }*/
            if($uniacid['type'] == 2 ){
                //开团的
                $da = [
                    'weid'=>$uniacid['weid'],
                    'uid'=>$uniacid['userid'],
                    'lessonid'=>$uniacid['dataid'],
                    'groupnum'=>1,
                    'status'=>2,
                    'createtime'=>time(),
                ];
                pdo_insert('goouc_class_user_group',$da);
                $groupid = pdo_insertid();
                $daa = [
                    'weid'=>$uniacid['weid'],
                    'groupid'=>$groupid,
                    'uid'=>$uniacid['userid'],
                    'isgrouper'=>1,
                    'lessonid'=>$uniacid['dataid'],
                    'createtime'=>time(),
                    'out_trade_no'=>$uniacid['out_trade_no']
                ];
                pdo_insert('goouc_class_user_group_info',$daa);

            }elseif($uniacid['type'] == 3){
                //参团
                $daa = [
                    'weid'=>$uniacid['weid'],
                    'groupid'=>$uniacid['groupid'],
                    'uid'=>$uniacid['userid'],
                    'isgrouper'=>2,
                    'lessonid'=>$uniacid['dataid'],
                    'createtime'=>time(),
                    'out_trade_no'=>$uniacid['out_trade_no']
                ];
                $res = pdo_insert('goouc_class_user_group_info',$daa);
                if($res){
                    pdo_query('update  '.tablename('goouc_class_user_group').' set groupnum = groupnum + 1 where weid=:weid AND id=:id',array(':weid'=>$uniacid['weid'],':id'=>$uniacid['groupid']));
                }
                // $groupinfo = pdo_get('goouc_class_user_group',array('id'=>$uniacid['groupid']));
                // if($groupinfo['groupnum'] == $payset['groupnum']){
                //     pdo_query("update ".tablename('goouc_class_user_group')." set status = 1 where id = :id",array(':id'=>$uniacid['groupid']));
                //     //将参团的所有用户对课程进行报名
                //     $userlist = pdo_getall("goouc_class_user_group_info",array('weid'=>$uniacid['weid'],'groupid'=>$uniacid['groupid']));
                //     foreach($userlist as $k=>$v){
                //         $arr = [
                //             'weid'=>$uniacid['weid'],
                //             'uid'=>$v['uid'],
                //             'lessonid'=>$v['lessonid'],
                //             'type'=>5,
                //             'old_money'=>$uniacid['true_money'],
                //             'createtime'=>time()
                //         ];
                //         pdo_insert('goouc_class_user_signup',$arr);
                //         pdo_query("update ".tablename('goouc_class_lesson')." set study_num = study_num + 1 where id = :id",array(':id'=>$uniacid['dataid']));
                //     }
                // }
                $groupinfo = pdo_get('goouc_class_user_group',array('id'=>$uniacid['groupid']));
                if($groupinfo['groupnum'] >= $payset['groupnum']){
                    pdo_query("update ".tablename('goouc_class_user_group')." set status = 1 where id = :id",array(':id'=>$uniacid['groupid']));
                    //将参团的所有用户对课程进行报名
                    $userlist = pdo_getall("goouc_class_user_group_info",array('weid'=>$uniacid['weid'],'groupid'=>$uniacid['groupid']));
                    foreach($userlist as $k=>$v){

                        $oldid = pdo_getcolumn('goouc_class_user_signup',array('weid' => $uniacid['weid'], 'uid' => $v['uid'], 'lessonid' => $v['lessonid'], 'type' => 5), 'id');
                        if(!empty($oldid)){
                            continue;
                        }
                        
                        $arr = [
                            'weid'=>$uniacid['weid'],
                            'uid'=>$v['uid'],
                            'lessonid'=>$v['lessonid'],
                            'type'=>5,
                            'old_money'=>$uniacid['true_money'],
                            'createtime'=>time()
                        ];
                        pdo_insert('goouc_class_user_signup',$arr);
                        pdo_query("update ".tablename('goouc_class_lesson')." set study_num = study_num + 1 where id = :id",array(':id'=>$uniacid['dataid']));
                    }
                }
            }elseif($uniacid['type'] == 4){
                $res = pdo_query("update ".tablename('goouc_class_user_exchange')." set status = 1 where id = :id",array(':id'=>$uniacid['usercardid']));
                if($res){
                    //使用兑课券购买
                    $daa = [
                        'weid'=>$uniacid['weid'],
                        'uid'=>$uniacid['userid'],
                        'lessonid'=>$uniacid['dataid'],
                        'type'=>4,
                        'pay_money'=>$uniacid['all_money'],
                        'createtime'=>time()
                    ];
                    pdo_insert('goouc_class_user_signup',$daa);
                    pdo_query("update ".tablename('goouc_class_lesson')." set study_num = study_num + 1 where id = :id",array(':id'=>$uniacid['dataid']));
                }
            }else{
                //直接购买
                $daa = [
                    'weid'=>$uniacid['weid'],
                    'uid'=>$uniacid['userid'],
                    'lessonid'=>$uniacid['dataid'],
                    'type'=>4,
                    'pay_money'=>$uniacid['all_money'],
                    'createtime'=>time()
                ];
                pdo_insert('goouc_class_user_signup',$daa);
                pdo_query("update ".tablename('goouc_class_lesson')." set study_num = study_num + 1 where id = :id",array(':id'=>$uniacid['dataid']));
            }
        }
	}
}
$return = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
echo $return;die();
?>